package com.nasarius.bank.scorecard.web.controller.admin;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomCollectionEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.nasarius.bank.scorecard.dao.BankFactorsActualDAO;
import com.nasarius.bank.scorecard.dao.BankFactorsDAO;
import com.nasarius.bank.scorecard.dao.FactorsDAO;
import com.nasarius.bank.scorecard.domain.Factors;
import com.nasarius.bank.vo.FactorsVO;

@Controller
public class FactorDetailsController {
	 /** Logger that is available to subclasses */
	 protected final Log LOG = LogFactory.getLog(getClass());
		@Autowired
		private FactorsDAO factorsDAO;
		@Autowired
		private BankFactorsActualDAO bankFactorsActualDAO;
		@Autowired
		private BankFactorsDAO bankFactorsDAO;		
		@InitBinder
		protected void initBinder(ServletRequestDataBinder binder) {
				binder.registerCustomEditor(List.class,new CustomCollectionEditor(List.class));
		}		
		/**
		 * For every request for this controller, this will create a FactorDetails
		 * instance for the form .
		 */
		@ModelAttribute
		public FactorsVO newRequest(@RequestParam(required = false) String factorID,Model model) {
			List<String> factorTypes= new ArrayList<String>();
			factorTypes.add("Qualitative");
			factorTypes.add("Quantitative");
			model.addAttribute("factorTypes", factorTypes);
			FactorsVO factorsVO = new FactorsVO();
			factorsVO.setFactors(factorsDAO.selectAllFactors());
			return factorsVO;
		}
		   /**
	     * <p>Person form request.</p>
	     * 
	     * <p>Expected HTTP GET and request '/factordetails/view'.</p>
	     */
	    @RequestMapping(value="/factordetails/view", method=RequestMethod.GET)
	    public void form() {}
	    
	    public void setFactorsDAO(FactorsDAO factorsDAO) {
			this.factorsDAO = factorsDAO;
		}
	    
	    /**
	     * <p>Saves a person.</p>
	     * 
	     * <p>Expected HTTP POST and request '/factordetails/add'.</p>
	     */
	    @RequestMapping(value="/factordetails/add", method=RequestMethod.POST)
	    public FactorsVO add(FactorsVO factorsVO, Model model) {
	    	LOG.info("Add new factor");
	    	factorsVO.getFactors().add(new Factors());
	    	return factorsVO;
	    }
	    
	    public void setBankFactorsActualDAO(BankFactorsActualDAO bankFactorsActualDAO) {
			this.bankFactorsActualDAO = bankFactorsActualDAO;
		}
		public void setBankFactorsDAO(BankFactorsDAO bankFactorsDAO) {
			this.bankFactorsDAO = bankFactorsDAO;
		}
		@RequestMapping(value="/factordetails/update", method=RequestMethod.POST)
	    public void update(FactorsVO factorsVO, Model model) {
	    	for(Factors factors:factorsVO.getFactors()){
	    		LOG.info("factor"+factors.getFactorname());
	    		LOG.info("bank"+factors.getFactortype());
	    		if(factors.getFactorid()!=null)
	    			factorsDAO.updateFactorsByPrimaryKey(factors);
    			else
    				factorsDAO.insertFactors(factors);
	    	}
	    }
	    @RequestMapping(value="/factordetails/delete", method=RequestMethod.GET)
	    public String delete(@RequestParam(required = false) Integer factorid, Model model) {
	    			bankFactorsDAO.deleteBankFactorsByFactorID(factorid);
	    			bankFactorsActualDAO.deleteBankFactorsByFactorID(factorid);
	    			factorsDAO.deleteFactorsByPrimaryKey(factorid);
	    			return "redirect:/factordetails/view.html"; 
	    }
}
